package com.ylbh.modules.version.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylbh.common.pojo.version.dto.AddAppVersionDTO;
import com.ylbh.common.pojo.version.dto.AppVersionDTO;
import com.ylbh.common.pojo.version.vo.VersionVO;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.version.entity.ShoppingAndroidApp;
import com.ylbh.modules.version.mapper.ShoppingAndroidAppMapper;
import com.ylbh.modules.version.service.ShoppingAndroidAppService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;


@Service
public class ShoppingAndroidAppServiceImpl extends ServiceImpl<ShoppingAndroidAppMapper, ShoppingAndroidApp> implements ShoppingAndroidAppService {

    @Autowired
    private ShoppingAndroidAppMapper shoppingAndroidAppMapper;

    /**
     * 获取app版本控制列表信息
     * @param appVersionDTO
     * @return
     */
    @Override
    public PageUtils queryVersionList(AppVersionDTO appVersionDTO) {
        Page<Override> page = new Page<>(appVersionDTO.getPage(), appVersionDTO.getLimit());
        List<ShoppingAndroidApp> versionList = shoppingAndroidAppMapper.selectVersionList(appVersionDTO, page);
        Page<ShoppingAndroidApp> page1 = new Page<>();
        page1.setTotal(page.getTotal()).setRecords(versionList);
        return new PageUtils(page1);
    }

    /**
     * 获取app版本控制详情信息
     * @param id
     * @return
     */
    @Override
    public ShoppingAndroidApp getVersionById(Long id) {
        ShoppingAndroidApp version = shoppingAndroidAppMapper.selectVersionById(id);
        return version;
    }

    /**
     * 新的版本号
     * @param appType
     */
    @Override
    public VersionVO getVersionByType(Long appType) {
        VersionVO version = shoppingAndroidAppMapper.selectVersionByAppType(appType);
        if (null != version) {
            if (!StringUtils.isEmpty(version.getAssistVer()) && !StringUtils.isEmpty(version.getMasterVer())) {
                // 根据该类型最新的版本信息来生成下个版本的版本号
                Integer assIsVer = Integer.parseInt(version.getAssistVer()) + 2;
                String masterVer = version.getMasterVer();
                // 计算主版本号，在原有的基础上加1
                String[] split = masterVer.split("\\.");
                int i = 0,i1 = 0,i2 = 0;
                String resultMastVer = "";
                if (split.length == 3) {
                    if (Integer.parseInt(split[2]) >= 9) {
                        if (Integer.parseInt(split[1]) >= 9){
                            i = Integer.parseInt(split[0]) + 1;
                        }else {
                            i = Integer.parseInt(split[0]);
                            i1 = Integer.parseInt(split[1]) + 1;
                        }
                        resultMastVer = i + "." + i1 + "." + i2;
                    }else {
                        i2 = Integer.parseInt(split[2]) + 1;
                        resultMastVer = split[0] + "." + split[1] + "." + i2;
                    }
                }
                version.setNewMasterVer(resultMastVer);
                version.setNewAssistVer(assIsVer);
                return version;
            }
        }
        return null;
    }

    /**
     * 新增对应的app版本信息
     * @param addAppVersionDTO
     */
    @Override
    public void addAppVersion(AddAppVersionDTO addAppVersionDTO) {
        addAppVersionDTO.setDate(new Date());
        shoppingAndroidAppMapper.insertAppVersion(addAppVersionDTO);
    }
}
